package com.joy.web.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.joy.common.model.vo.JsTreeVO;
import com.joy.web.admin.mapper.CommonCdMapper;
import com.joy.web.admin.service.ICommonCdService;
import com.joy.common.constant.CommonCons;
import com.joy.web.admin.entity.CommonCd;
import com.joy.web.base.service.impl.IBaseServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjzhao
 * @since 2019-09-23
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class CommonCdServiceImpl extends IBaseServiceImpl<CommonCdMapper, CommonCd> implements ICommonCdService {

    @Override
    public List<JsTreeVO> findAllCommonCdTree() throws Exception {
        List<CommonCd> commonCdList = findAllCommonCd();
        //生成 jstree 数据结构

        List<JsTreeVO> jsTreeVOList = this.getJsTree(commonCdList);
        return jsTreeVOList;
    }

    @Override
    @Cacheable(value= CommonCons.MyCache.COMMON, key = CommonCons.CommonCacheKey.COMMON_CD)
    public List<CommonCd> findAllCommonCd() throws Exception {
        //获取全部有效的数据字典
        QueryWrapper<CommonCd> commonCdQ = new QueryWrapper<>();
        commonCdQ.lambda()
                .eq(CommonCd::getDelYn, CommonCons.FlagType.N);
        List<CommonCd> commonCdList = this.list(commonCdQ);
        return commonCdList;
    }

    @Override
    public Optional<CommonCd> findByChildCd(String childCd) throws Exception {
        List<CommonCd> allCommonCd = findAllCommonCd();
        return allCommonCd.stream()
                .filter(commonCd -> childCd.equals(commonCd.getChildCd()))
                .findAny();
    }

    @Override
    public List<CommonCd> findListByParentCd(String parentCd) throws Exception {
        List<CommonCd> allCommonCd = findAllCommonCd();
        return allCommonCd.stream()
                .filter(commonCd -> parentCd.equals(commonCd.getParentCd()))
                .collect(Collectors.toList());
    }


    @Override
    @Transactional(readOnly = false)
    @CacheEvict(value= CommonCons.MyCache.COMMON, allEntries = true, key = CommonCons.CommonCacheKey.COMMON_CD)
    public boolean update(CommonCd commonCd) throws Exception {
        boolean updateFlag = this.lambdaUpdate()
                .set(CommonCd::getChildCd, commonCd.getChildCd())
                .set(CommonCd::getChildCdNm, commonCd.getChildCdNm())
                .set(CommonCd::getParentCd, commonCd.getParentCd())
                .eq(CommonCd::getChildCd, commonCd.getChildCd())
                .eq(CommonCd::getDelYn, CommonCons.FlagType.N)
                .update();
        return updateFlag;
    }

    @Override
    @Transactional(readOnly = false)
    @CacheEvict(value= CommonCons.MyCache.COMMON, allEntries = true, key = CommonCons.CommonCacheKey.COMMON_CD)
    public boolean delete(String childCd) throws Exception {
        boolean delFlag = this.lambdaUpdate()
                .set(CommonCd::getDelYn, CommonCons.FlagType.Y)
                .eq(CommonCd::getChildCd, childCd)
                .eq(CommonCd::getDelYn, CommonCons.FlagType.N)
                .update();
        this.lambdaUpdate()
                .set(CommonCd::getDelYn, CommonCons.FlagType.Y)
                .eq(CommonCd::getParentCd, childCd)
                .eq(CommonCd::getDelYn, CommonCons.FlagType.N)
                .update();

        return delFlag;
    }


    /**
     * @Description 
     * @param commonCdList
     * @return
     * @Author yjzhao 
     * @Date 2019/9/23 14:23
     **/
    private List<JsTreeVO> getJsTree(List<CommonCd> commonCdList) {
        List<JsTreeVO> rtnList = Lists.newArrayList();
        JsTreeVO jsTreeVORoot = new JsTreeVO();
        String rootNodeNm = "ROOT";
        jsTreeVORoot.setId(rootNodeNm);
        jsTreeVORoot.setParent("#");
        jsTreeVORoot.setText("根目录");
        jsTreeVORoot.setState("selected");
        rtnList.add(jsTreeVORoot);
        commonCdList.forEach(commonCd -> {
            JsTreeVO jsTreeVO = new JsTreeVO();
            jsTreeVO.setId(commonCd.getChildCd());
            String parentCd = commonCd.getParentCd();
            if (StrUtil.isEmpty(parentCd)) {
                parentCd = rootNodeNm;
            }
            jsTreeVO.setParent(parentCd);
            jsTreeVO.setText(commonCd.getChildCdNm());
            rtnList.add(jsTreeVO);
        });
        return rtnList;
    }
}
